package com.fenghuajueli.module_user.activity.forget;

import android.graphics.Color;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;

import androidx.lifecycle.Observer;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.libbasecoreui.utils.QuickClickUtils;
import com.fenghuajueli.libbasecoreui.viewmodel.BaseViewModel;
import com.fenghuajueli.module_route.UserModuleRoute;
import com.fenghuajueli.module_user.R;
import com.fenghuajueli.module_user.activity.UserViewModelBaseActivity;
import com.fenghuajueli.module_user.databinding.ActivityForgetPasswordBinding;
import com.fenghuajueli.module_user.model.UserViewModel;
import com.fenghuajueli.module_user.network.result.TemplateResult;

@Route(path = UserModuleRoute.USER_FORGET_PASSWORD)
public class ForgetPasswordActivity extends UserViewModelBaseActivity implements View.OnClickListener {

    private boolean pwdIsShow = false;
    private boolean pwdIsShow2 = false;

    ActivityForgetPasswordBinding binding;

    private final UserViewModel userViewModel = new UserViewModel();

    @Override
    public BaseViewModel setBaseViewModel() {
        return userViewModel;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityForgetPasswordBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.btnSendCode.setOnClickListener(this);
        binding.btnComplete.setOnClickListener(this);
        binding.ivClose.setOnClickListener(this);
        binding.ivChangePwdStatus.setOnClickListener(this);
        binding.ivChangePwdStatus2.setOnClickListener(this);

        userViewModel.verificationCodeTime.observe(this, time -> {
            binding.btnSendCode.setText(time + "s");
            binding.btnSendCode.setEnabled(false);
            if (time == 0) {
                binding.btnSendCode.setEnabled(true);
                binding.btnSendCode.setText("重新获取");
            }
        });

        userViewModel.phoneIsRegister.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (!aBoolean){
                    userViewModel.getVerificationCode(ForgetPasswordActivity.this, binding.etInputPhone.getText().toString().trim());
                }else {
                    ToastUtils.showShort("手机号未注册");
                }
            }
        });
        userViewModel.templateResult.observe(this, result -> {
            if (result instanceof TemplateResult.Success){
                String themeColors = ((TemplateResult.Success) result).getData().getTheme_color().getTheme_colors();
                binding.btnComplete.getBackground().setTint(Color.parseColor(themeColors));
                binding.btnSendCode.getBackground().setTint(Color.parseColor(themeColors));
            }
        });
    }


    @Override
    public void onClick(View view) {
        if (QuickClickUtils.isFastClick()) {
            return;
        }
        int viewId = view.getId();
        if (viewId == R.id.ivClose) {
            finish();
        } else if (viewId == R.id.btnComplete) {
            changePwd();
        } else if (viewId == R.id.btnSendCode) {
            if (userViewModel != null) {
                userViewModel.checkPhoneIsRegister(this, binding.etInputPhone.getText().toString().trim());
            }
        } else if (viewId == R.id.ivChangePwdStatus) {
            if (pwdIsShow) {
                binding.etInputPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                binding.ivChangePwdStatus.setImageResource(R.mipmap.aa_icon_to_view);
            } else {
                binding.etInputPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                binding.ivChangePwdStatus.setImageResource(R.mipmap.aa_icon_close_look_at);
            }
            pwdIsShow = !pwdIsShow;
            binding.etInputPassword.postInvalidate();
        } else if (viewId == R.id.ivChangePwdStatus2) {
            if (pwdIsShow2) {
                binding.etConfirmInputPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                binding.ivChangePwdStatus2.setImageResource(R.mipmap.aa_icon_to_view);
            } else {
                binding.etConfirmInputPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                binding.ivChangePwdStatus2.setImageResource(R.mipmap.aa_icon_close_look_at);
            }
            pwdIsShow2 = !pwdIsShow2;
            binding.etConfirmInputPassword.postInvalidate();
        }
    }


    private void changePwd() {
        if (userViewModel != null) {
            userViewModel.editPassWord(this, binding.etInputPhone.getText().toString().trim(), binding.etInputPassword.getText().toString().trim(),
                    binding.etConfirmInputPassword.getText().toString().trim(), binding.etInputCode.getText().toString().trim());
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}